library(estimatr)
library(ggplot2)
library(plyr)
library(dplyr)
library(dotwhisker)
library(gridExtra)
library(tidyverse)


# Figure 4: Support for US Presidential Decision for Nuclear Retaliation

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_1/")

COMdat <- read.csv("Paper_1_Data_Clean_tidy.csv", stringsAsFactors = FALSE)

# Summarizing based on support for nuclear retaliation

COMdat15 <- ddply(COMdat, c("Nationality", "Japan", "Nuclear"), 
                  summarise, N = sum(!is.na(SupNucRetal)), 
                  xbar = mean(SupNucRetal, na.rm=T), 
                  stddev= sd(SupNucRetal, na.rm=T), 
                  stderr=stddev/sqrt(N) 
) 

# Labeling the Nationalities
NationalityLabeller <- as_labeller(c(
  '1' = "USA",            
  '2' = "Japan",         
  '3' = "S. Korea"
))



# COMdat15 <-
#   COMdat %>%
#   group_by(Nationality, Japan, Nuclear) %>%
#   summarize(tidy(lm_robust(cbind(SupNucRetal) ~ 1, data = cur_data())))



COMdat15$Label <- round(COMdat15$xbar,2)

# Plots for Figure 4

plot1e <- ggplot(subset(COMdat15, Nuclear==0 & Japan==0), aes(x=1,y=xbar, fill = factor(Nationality)))+ coord_cartesian(ylim=c(0,.6)) +
  ggtitle("Conventional Attack on Busan, South Korea") + ylab("Proportion of Respondents") +
  facet_wrap(~Nationality, labeller = NationalityLabeller) + 
  geom_bar(stat = "identity") + theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5)) + 
  geom_errorbar(aes(ymin=xbar-stderr*1.96, ymax=xbar+stderr*1.96), width=.2,position=position_dodge(.9)) +
  geom_text(aes(label=Label),  y=0.05) +
  scale_fill_manual(guide=F, values=c("forestgreen", "indianred2","steelblue2")) +
  scale_x_discrete(name = NULL) + theme_bw() +
  theme(axis.text.x = element_text(angle=90), legend.position = "none", plot.title=element_text(size=9)) 

plot2e <- ggplot(subset(COMdat15, Nuclear==1 & Japan==0), aes(1,y=xbar, fill = factor(Nationality)))  + coord_cartesian(ylim=c(0,.6)) +
  ggtitle("Nuclear Attack on Busan, South Korea") + ylab("") +
  facet_wrap(~Nationality, labeller = NationalityLabeller) + 
  geom_bar(stat = "identity") + theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5)) + 
  geom_errorbar(aes(ymin=xbar-stderr*1.96, ymax=xbar+stderr*1.96), width=.2,position=position_dodge(.9)) +
  geom_text(aes(label=Label),  y=0.05) +
  scale_fill_manual(guide=F, values=c("forestgreen", "indianred2","steelblue2")) +
  scale_x_discrete(name = NULL) + theme_bw()+
  theme(axis.text.x = element_text(angle=90), legend.position = "none", plot.title=element_text(size=9))

plot3e <- ggplot(subset(COMdat15, Nuclear==0 & Japan==1), aes(1,y=xbar, fill = factor(Nationality)))  + coord_cartesian(ylim=c(0,.6)) +
  ggtitle("Conventional Attack on Nagoya, Japan") + ylab("Proportion of Respondents") +
  facet_wrap(~Nationality, labeller = NationalityLabeller) + 
  geom_bar(stat = "identity") + theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5)) + 
  geom_errorbar(aes(ymin=xbar-stderr*1.96, ymax=xbar+stderr*1.96), width=.2,position=position_dodge(.9)) +
  geom_text(aes(label=Label),  y=0.05) +
  scale_fill_manual(guide=F, values=c("forestgreen", "indianred2","steelblue2")) +
  scale_x_discrete(name = NULL) + theme_bw()+
  theme(axis.text.x = element_text(angle=90), legend.position = "none", plot.title=element_text(size=9))

plot4e <- ggplot(subset(COMdat15, Nuclear==1 & Japan==1), aes(1,y=xbar, fill = factor(Nationality))) + coord_cartesian(ylim=c(0,.6)) +
  ggtitle("Nuclear Attack on Nagoya, Japan") + ylab("") +
  facet_wrap(~Nationality, labeller = NationalityLabeller) + 
  geom_bar(stat = "identity") + theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5)) + 
  geom_errorbar(aes(ymin=xbar-stderr*1.96, ymax=xbar+stderr*1.96), width=.2,position=position_dodge(.9)) +
  geom_text(aes(label=Label),  y=0.05) +
  scale_fill_manual(guide=F, values=c("forestgreen", "indianred2","steelblue2")) +
  scale_x_discrete(name = NULL) + theme_bw() +
  theme(axis.text.x = element_text(angle=90), legend.position = "none", plot.title=element_text(size=9))

g<-grid.arrange(plot1e, plot2e, plot3e, plot4e, ncol=2)


ggsave("Image_4.png", plot=g, width=2500, height=2500, units=c("px"))



